package cn.com.edu.nju.ecrec.dataProcess;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.*;

import cn.com.edu.nju.ecrec.model.*;
import cn.com.edu.nju.ecrec.util.JPAFactory;

public class UserInfoExtraction {
	
	static void processInfo() {
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(
					"info.txt")));

			EntityManager em = JPAFactory.getManager();
			String line = br.readLine();
			while (line != null) {
				em.getTransaction().begin();
				
				UserInfo info = new UserInfo();
				info.setUserID(line);
				
				//get browser and os info
				String[] infos = getBrowOsInfo(br.readLine());
				info.setOs(infos[1]);
				if(infos[0].indexOf("MSIE") > -1){
					String[] browser = infos[0].split(" ");
					info.setBrowser(browser[0]);
					info.setVersion(browser[1]);
				}
				else if(infos[0].indexOf('/') > -1){
					info.setBrowser(infos[0].substring(0,infos[0].indexOf('/')));
					info.setVersion(infos[0].substring(infos[0].indexOf('/')+1,infos[0].length()));
				}
				else{
					info.setBrowser(infos[0]);
					info.setVersion("-1.0");
				}
				System.out.println(info.getBrowser());
				System.out.println(info.getVersion());
				
				String versionStr = br.readLine();
				if(info.getVersion().equals("-1.0")){
					if(versionStr.equals(""))
						info.setVersion("-1.0");
					else
						info.setVersion(versionStr);
				}
				
				info.setPlatform(br.readLine());
				info.setBrowserLanguage(br.readLine());
				info.setSystemLanguage(br.readLine());
				info.setUserLanguage(br.readLine());
				info.setCountry(br.readLine());
				info.setRegion(br.readLine());
				info.setCity(br.readLine());
				
				//get url info
				String url = br.readLine();
				System.out.println(url);
				if(url.indexOf('?') > -1){
					info.setRefer_url(url.substring(0,url.indexOf('?')));
					info.setParas(getParas(url.substring(url.indexOf('?')+1,url.length())));
				}
				else
					info.setRefer_url(url);
				
				String dataStr = br.readLine();
				if(dataStr.equals("null"))
					info.setDate(null);
				else
					info.setDate(new Date(Long.parseLong(dataStr)));
				
				em.persist(info);
				em.getTransaction().commit();
				br.readLine();
				line = br.readLine();
			}
			em.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static String[] getBrowOsInfo(String userAgentInfo){
		System.out.println(userAgentInfo);
		if(userAgentInfo.equals("null") || userAgentInfo.equals(""))
			return new String[]{"Unknown Browser", "Unknown OS"};
		String[] result = new String[2];
		String info = userAgentInfo.toUpperCase();  
        
		if(info.indexOf("(") > -1 && info.indexOf(")") > -1 && info.indexOf("(", info.indexOf(")")) > -1){
			info = info.substring(0, info.indexOf("(", info.indexOf(")")) - 1);
			System.out.println("info:"+info);
		}
		
		if(info.indexOf("(") == -1)
			return new String[]{"Unknown Browser", "Unknown OS"};
		
        String[] strInfo = info.substring(info.indexOf("(") + 1,  
                info.indexOf(")") - 1).split(";");  
        if ((info.indexOf("MSIE")) > -1) {  
            result[0] = strInfo[1].trim();  
            if(strInfo.length > 2)
            	result[1] = strInfo[2].trim();  
            else
            	result[1] = "Unknown OS";
        } else {  
            String[] str = info.split(" ");  
            if (info.indexOf("NAVIGATOR") < 0 && info.indexOf("FIREFOX") > -1) {  
            	result[0] = str[str.length - 1].trim(); 
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else if ((info.indexOf("OPERA")) > -1) {  
            	result[0] = str[0].trim();  
            	result[1] = strInfo[0].trim();  
            } else if (info.indexOf("CHROME") < 0  
                    && info.indexOf("SAFARI") > -1) {  
            	result[0] = str[str.length - 1].trim();  
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else if (info.indexOf("CHROME") > -1) {  
            	result[0] = str[str.length - 2].trim();  
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else if (info.indexOf("NAVIGATOR") > -1) {  
            	result[0] = str[str.length - 1].trim();  
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else if (info.indexOf("APPLEWEBKIT") > -1) {  
            	result[0] = str[str.length - 1].trim();  
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else {  
            	result[0] = "Unknown Browser";  
            	result[1] = "Unknown OS";  
            }  
        }  
        System.out.println(result[0]+" "+result[1]);
        return result;
	}
	
	static List<RefPara> getParas(String paraStr){
		List<RefPara> rps = new ArrayList<RefPara>();
		String[] paras = paraStr.split("&");
		for(String para : paras){
			if(para.indexOf('=') > -1){
				String[] keyValue = para.split("=");
				if(keyValue.length == 0)
					continue;
				RefPara rp = new RefPara();
				rp.setParaKey(keyValue[0]);
				if(keyValue.length > 1)
					rp.setParaValue(keyValue[1]);
				else
					rp.setParaValue(null);
				rps.add(rp);
			}
		}
		return rps;
	}
	
	public static void main(String[] args) {
		processInfo();
	}
}